<template>
<el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="30%">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" label-width="80px">
        <el-form-item label="所属公司" prop="company">
            <picker-view v-model="ruleForm.company_id" type="company" placeholder="请选择公司"></picker-view>
        </el-form-item>
        <el-form-item label="审批类型" prop="timing">
            <picker-view v-model="ruleForm.timing" type="timing" placeholder="请选择审核类型"></picker-view>
        </el-form-item>
        <el-form-item label="排序">
            <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="上下线">
            <el-radio-group v-model="ruleForm.is_valid">
                <el-radio :label="1">上线</el-radio>
                <el-radio :label="0">下线</el-radio>
            </el-radio-group>
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import {
    readApi,
    createApi,
    saveApi,
} from "@/api/audit/config";

import pickerView from "@/components/picker/select.vue";

const ruleForm = {
    company_id: '',
    timing: '',
    sort: 0,
    is_valid: 1
};

export default {
    components: {
        pickerView
    },
    data() {
        return {
            dialog: {
                title: "",
                visible: false,
                type: "",
            },
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                company_id: [{
                    required: true,
                    message: '请选择所属公司',
                    trigger: 'change'
                }],
                timing: [{
                    required: true,
                    message: '请选择审核类型',
                    trigger: 'change'
                }]
            },
        };
    },
    methods: {
        showAddView() {
            let dialog = {
                title: "添加审核规则",
                visible: true,
                type: "create",
            };
            this.dialog = dialog;
            // 初始化表单
            this.ruleForm = Object.assign({}, ruleForm);
        },
        showSaveView(row) {

            let dialog = {
                title: "编辑审核规则",
                visible: true,
                type: "save",
            };
            this.dialog = dialog;
            // 初始化表单
            this.ruleForm = Object.assign({}, row);
        },
        submit() {
            this.$refs["ruleForm"].validate((valid) => {
                if (!valid) return false;

                if (this.dialog.type == "create") {
                    createApi(this.ruleForm)
                        .then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("getTableData", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
                } else {
                    saveApi(this.ruleForm)
                        .then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("getTableData", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
                }
            });
        },
        closeDialog() {
            this.ruleForm = Object.assign({}, ruleForm);
        }
    },
};
</script>
